<template>
  <view>
    <view class="goods-list">
      <view v-for="(item,index) in goodsList" :key="index" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  
      
  </view>
</template>

<script>
  export default {
    data() {
      return {
        //请求参数对象
        queryObj: {
          //关键词
          query: '',
          //商品分类Id
          cid: '',
          //页码
          pagenum: 1,
          //每页显示条数
          pagesize: 10,
         } ,
         //商品列表数据
         goodsList: [],
         //总数量
         total: 0,
         //节流阀
         isLoading: false
         
        
      };
    },
    onLoad(options) {
      //将页面参数转存到data中
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      //发起请求
      this.getGoodsList()
    },
    methods: {
      //获取商品列表数据
      async getGoodsList() {
        //开启节流阀
        this.isLoading = true
        //发起请求
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        //关闭节流阀
        this.isLoading = false
        //判断请求是否成功
        if (res.meta.status !== 200) return uni.$showMsg()
        this.total = res.message.total
        //为data中的goodsList数组添加数据
        this.goodsList = [...this.goodsList, ...res.message.goods]
      },
      //点击跳转到商品详情页面
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
     if(this.isLoading) return
      this.queryObj.pagenum += 1
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.total = 0
      this.goodsList = []
      this.getGoodsList()
      uni.stopPullDownRefresh()
    }
  }
</script>

<style lang="scss">
  
    
  

</style>
